#include<bits/stdc++.h>
using namespace std;

int gg,g[50],ncnt;
int rs,a[50],n,cnt;
list<int> s[10];
vector<int> ccnt;
int ra[50],x[50],y[50],ss[10];
int xx[50],yy[50];
int u[5000],v[5000],uv;
int se[10][10],ge[10][10];
int ss1,ss2,ss3;

int dx[8]={1,1,1,-1,-1,-1,0,0};
int dy[8]={1,0,-1,1,0,-1,1,-1};

void init(){
    memset(se,0,sizeof(se));
    memset(ge,0,sizeof(ge));
    for(int i=1;i<=7;i++){
        s[i].clear();
    }
    for(int i=1;i<=n;i++){
        se[x[i]][y[i]]=ra[i];
    }

    return;
}

bool check(){
    for(int i=1;i<=uv;i++){
        for(int j=0;j<8;j++){
            if(se[xx[u[i]]+dx[j]][yy[u[i]]+dy[j]]==v[i]){
                return false;
            }
        }
    }
    for(int j=1;j<=7;j++){
        ccnt.clear();
        for(int i=1;i<=7;i++){
            if(g[se[i][j]]){
                ccnt.push_back(i);
            }
        }
        if(ccnt.size()==0){
            return false;
        }
        ge[ccnt[rand()%ccnt.size()]][j]=1;
    }

    return true;
}

int main(){
    freopen("set.txt","r",stdin);
    scanf("%d %d %d %d %d %d %d",&rs,&n,&gg,&ss1,&ss2,&ss3,&uv);
    for(int i=1;i<=ss1+ss2+ss3;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=7;i++){
        ss[i]=7;
    }
    for(int i=1;i<=gg;i++){
        scanf("%d",&g[0]);
        g[g[0]]=1;
        g[0]=0;
    }
    for(int i=1;i<=uv;i++){
        scanf("%d %d",&u[i],&v[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d %d %d",&ra[i],&x[i],&y[i]);
        ss[x[i]]--;
        se[x[i]][y[i]]=ra[i];
    }
    srand(rs);
    for(ncnt=1;;ncnt++){
        init();
        random_shuffle(a+1,a+ss1+1);
        random_shuffle(a+ss1+1,a+ss1+ss2+1);
        random_shuffle(a+ss1+ss2+1,a+ss1+ss2+ss3+1);
        cnt=0;
        for(int i=1;i<=7;i++){
            while(s[i].size()<ss[i]){
                s[i].push_back(a[++cnt]);
            }
        }
        for(int i=1;i<=7;i++){
            for(int j=1;j<=7;j++){
                if(se[i][j]==0){
                    se[i][j]=s[i].front();
                    s[i].pop_front();
                }
            }
        }
        for(int i=0;i<7;i++){
            for(int j=1;j<=50;j++){
                swap(se[i+1][rand()%7+1],se[i+1][rand()%7+1]);
            }
        }
        for(int i=1;i<=7;i++){
            for(int j=1;j<=7;j++){
                xx[se[i][j]]=i;
                yy[se[i][j]]=j;
            }
        }
        for(int i=1;i<=n;i++){
            printf("%d %d %d\n",se[xx[ra[i]]][yy[ra[i]]],xx[ra[i]],y[i]);
            swap(se[xx[ra[i]]][yy[ra[i]]],se[xx[ra[i]]][y[i]]);
            swap(yy[ra[i]],yy[se[xx[ra[i]]][yy[ra[i]]]]);
        }

        printf("Random Generation %d Completed\n",ncnt);
        if(check()){
            printf("Random Generation Check %d Completed with result: green\n",ncnt);
            break;
        }else{
            printf("Random Generation Check %d Completed with result: red\n",ncnt);
        }
    }
    printf("\nFound Legal Generation at Random Generation %d\n",ncnt);
    printf("######## preview ########\n");
    printf("| G7 | G6 | G5 | G4 | G3 | G2 | G1 |\n");
    for(int i=1;i<=7;i++){
        printf("|");
        for(int j=1;j<=7;j++){
            if(!se[i][j]){
                printf(" -- |");
            }else if(ge[i][j]){
                printf("*%02d*|",se[i][j]);
            }else{
                printf(" %02d |",se[i][j]);
            }
        }
        printf("\n");
    }
    printf("Writing to CSV File...");
    freopen("seat.csv","w",stdout);
    printf("\" G7 \",\" G6 \",\" G5 \",\" G4 \",\" G3 \",\" G2 \",\" G1 \",\n");
    for(int i=1;i<=7;i++){
        for(int j=1;j<=7;j++){
            if(!se[i][j]){
                printf("\" -- \",");
            }else if(ge[i][j]){
                printf("\"*%02d*\",",se[i][j]);
            }else{
                printf("\" %02d \",",se[i][j]);
            }
        }
        printf("\n");
    }


    return 0;
}
